package com.hh.demo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Algorithm extends JFrame {

    private static final long serialVersionUID = 1L;
    public Algorithm() {
        JFrame frame=new JFrame();
        frame.setTitle("算法目录");
        frame.setLocation(500, 250);
        JMenuItem filesearch = new JMenuItem("查找");
        filesearch.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));//设定快捷键
        filesearch.addActionListener(new Search());
        JMenu file = new JMenu("文件");
        file.add(filesearch);

        JMenuBar mb = new JMenuBar();
        mb.add(file);
        frame.setJMenuBar(mb);
        JButton q1 = new JButton("线性时间选择问题");

        JButton q2 = new JButton("最优装载问题");

        JButton q3 = new JButton("符号三角形问题");

        JButton q4 = new JButton("石子合并问题");

        JButton q5 = new JButton("最小重量机器设计问题");

        ButtonGroup group = new ButtonGroup();
        group.add(q1);
        group.add(q2);
        group.add(q3);
        group.add(q4);
        group.add(q5);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 12));

        frame.add(q1);
        frame.add(q2);
        frame.add(q3);
        frame.add(q4);
        frame.add(q5);

        q1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Linearselect();
                System.out.println("\n");
            }
        });

        q2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Optimalloading();
                System.out.println("\n");
            }
        });

        q3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Symtriangle();
                System.out.println("\n");
            }
        });

        q4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new Stonemerge();
                    System.out.println("\n");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        q5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new Minweight();
                    System.out.println("\n");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        frame.setSize(600, 400);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);

    }

    class Search implements ActionListener
    {
        JFrame jf=null;
        JPanel jp1=null;
        JPanel jp2=null;
        JLabel jlb=null;
        JTextField jtf=null;
        JButton jb=null;
        @Override
        public void actionPerformed(ActionEvent e)
        {
            jf=new JFrame();
            jp1=new JPanel();
            jp2=new JPanel();
            jlb=new JLabel("请输入检索问题：");
            jtf=new JTextField(10);
            jb=new JButton("检索");
            jb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        check();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            });
            jp1.add(jlb);
            jp1.add(jtf);
            jp2.add(jb);
            jf.add(jp1);
            jf.add(jp2);
            jf.setLayout(new GridLayout(3,1));
            //给窗口设置标题
            jf.setTitle("算法目录检索");
            //设置窗体大小
            jf.setSize(300,200);
            //设置窗体初始位置
            jf.setLocation(200, 150);
            //设置当关闭窗口时
            jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            //显示窗体
            jf.setVisible(true);
            jf.setResizable(true);
        }
        public void check() throws Exception {
            if(jtf.getText().equals("线性时间选择问题")) {
                JOptionPane.showMessageDialog(null,"查找成功！","提示消息",JOptionPane.WARNING_MESSAGE);
                System.out.println("\n");
                new Linearselect();
            }
            else if(jtf.getText().equals("最优装载问题")) {
                JOptionPane.showMessageDialog(null,"查找成功！","提示消息",JOptionPane.WARNING_MESSAGE);
                System.out.println("\n");
                new Optimalloading();
            }
            else if(jtf.getText().equals("符号三角形问题")) {
                JOptionPane.showMessageDialog(null,"查找成功！","提示消息",JOptionPane.WARNING_MESSAGE);
                System.out.println("\n");
                new Symtriangle();
            }
            else if(jtf.getText().equals("石子合并问题")) {
                JOptionPane.showMessageDialog(null,"查找成功！","提示消息",JOptionPane.WARNING_MESSAGE);
                System.out.println("\n");
                new Stonemerge();
            }
            else if(jtf.getText().equals("最小重量机器设计问题")) {
                JOptionPane.showMessageDialog(null,"查找成功！","提示消息",JOptionPane.WARNING_MESSAGE);
                System.out.println("\n");
                new Minweight();
            }
            else {
                JOptionPane.showMessageDialog(null,"算法目录中不存在该问题！\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                clear();
            }
        }
        public void clear()
        {
            jtf.setText("");
        }
    }
}
